package com.example.team.rest.console.sys.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.example.common.tools.PageTools;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.team.rest.console.sys.entity.SysDictEntity;
import com.example.team.rest.console.sys.service.SysDictService;
import com.example.common.beans.Query;
import com.example.common.beans.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * @author zengyz
 * @email zengyuanzheng@qq.com
 * @date 2018-03-21 10:00:34
 */
@RestController
@RequestMapping("/sys/sysdict")
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    @Value("${server.port}")
    String port;

    /**
     * 列表
     */
    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public Result list(@RequestParam Map<String, Object> params,HttpServletRequest request){
        HttpSession session=request.getSession();
        session.setAttribute("name","测试session,我是在端口："+port+"保存的数据");
        //查询列表数据
        Query query = new Query(params);
        List<SysDictEntity> sysDictList = sysDictService.queryList(query);
        int total = sysDictService.queryTotal(query);
        PageTools pageTools = new PageTools(sysDictList, total, query.getLimit(), query.getPage());
        Map<String,Object> map=new HashMap<>();
        map.put("port",port);
        map.put("page",pageTools);
        return Result.ok(map);
    }


    /**
     * 信息
     */
    @RequestMapping(value = "/info/{uuid}",method = RequestMethod.GET)
    public Result info(@PathVariable("uuid") String uuid,HttpServletRequest request){
            SysDictEntity sysDict = sysDictService.queryObject(uuid);
        return Result.ok().put("sysDict", "端口："+port+",sessions数据："+request.getSession().getAttribute("name"));
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/info",method = RequestMethod.POST)
    public Result save(@RequestBody String sysDict){

        SysDictEntity sysDictEntity = JSON.parseObject(sysDict, SysDictEntity.class);
        sysDictService.save(sysDictEntity);
        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/info",method = RequestMethod.PUT)
    public Result update(@RequestBody String sysDict){
        SysDictEntity sysDictEntity = JSON.parseObject(sysDict, SysDictEntity.class);
        sysDictService.update(sysDictEntity);
        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/info",method = RequestMethod.DELETE)
    public Result delete(@RequestBody String[] uuids){
            sysDictService.deleteBatch(uuids);
        return Result.ok();
    }

    public static void main(String[] args) {
        SysDictEntity sysDictEntity=new SysDictEntity();
        sysDictEntity.setCreateTime(new Date());
        sysDictEntity.setKey("状态");
        sysDictEntity.setType(1);
        sysDictEntity.setValue("0:禁用,1:启用");
        sysDictEntity.setStatus(1);
        sysDictEntity.setRemark("状态类型");
        String s = JSON.toJSONString(sysDictEntity);
        System.out.println(s);
    }

}
